import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:get/get.dart';
import 'package:mziot/app/config/design.dart';
import 'package:mziot/app/modules/lighting/views/light_open_view.dart';
import 'package:mziot/app/modules/lighting/views/lightoff_view.dart';
import 'package:mziot/app/routes/app_pages.dart';
import 'package:mziot/app/tools/global_const.dart';
import 'package:mziot/app/tools/tools.dart';
import 'package:mziot/generated/assets.dart';
import 'package:mziot/generated/locales.g.dart';

import '../controllers/lighting_controller.dart';

class LightingView extends GetView<LightingController> {
  @override
  Widget build(BuildContext context) {
    return Obx(() {
      final isWifi = controller.isWifi.value;
      final isLock = controller.isLock.value;
      final isBluetooth = controller.isBluetooth.value;

      final disAble = controller.isGroup ? isLock : (!isWifi && !isBluetooth) || isLock;
      return WillPopScope(
        onWillPop: () async {
          Get.back();
          return Future.value(true);
        },
        child: ColorFiltered(
          colorFilter: ColorFilter.mode(
            Colors.white.withAlpha(disAble ? 100 : 0),
            BlendMode.hardLight,
          ),
          child: Stack(
            children: [
              Scaffold(
                extendBodyBehindAppBar: true,
                appBar: AppBar(
                  iconTheme: IconThemeData(color: Colors.white),
                  title: Text(
                    controller.name.value,
                    style: MFont.semi_Bold17.apply(color: Colors.white),
                  ),
                  backgroundColor: Colors.transparent,
                  systemOverlayStyle: SystemUiOverlayStyle.light,
                ),
                body: IgnorePointer(
                  ignoring: disAble,
                  child: Stack(
                    children: [
                      Image.asset(
                        Assets.imagesDeviceBack,
                        width: GlobalConst.screenWidth,
                        height: GlobalConst.screenHeight,
                        fit: BoxFit.fill,
                      ),
                      Container(
                        width: double.infinity,
                        child: Obx(() {
                          return controller.isClose.value ? LightOffView() : LightOpenView();
                        }),
                      ),
                      Center(
                        child: Obx(() {
                          final load = controller.isLoading.value;
                          if (!load) {
                            return Container();
                          }
                          return SpinKitCircle(
                            color: MColor.skin,
                            size: 40.0,
                          );
                        }),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      );
    });
  }
}

Widget lightingBottomView(LightingController controller) {
  return Container(
    color: Colors.white24,
    child: SafeArea(
      top: false,
      child: Container(
        height: 72,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          mainAxisSize: MainAxisSize.max,
          children: [
            Expanded(
              child: TextButton(
                onPressed: () {
                  Get.toNamed(
                    Routes.DEVICE_MANAGER,
                    arguments: {
                      'group': true,
                      'add': true,
                      'name': LocaleKeys.add_devices_group_title.tr,
                    },
                  );
                },
                child: Text(
                  LocaleKeys.add_devices_group_title.tr,
                  style: MFont.regular15.apply(color: Colors.white),
                ),
              ),
            ),
            IconButton(
              onPressed: () {
                throttle(() {
                  controller.openAction();
                });
              },
              icon: Image.asset(
                !controller.isClose.value ? Assets.imagesDeviceOpen : Assets.imagesDeviceClose,
                width: 50,
                height: 50,
                fit: BoxFit.fill,
              ),
              iconSize: 50,
            ),
            Expanded(
              child: TextButton(
                onPressed: () {
                  Get.back(result: 'scene');
                },
                child: Text(
                  LocaleKeys.lighting_scene.tr,
                  style: MFont.regular15.apply(color: Colors.white),
                ),
              ),
            ),
          ],
        ),
      ),
    ),
  );
}
